package net.quanter.samples.nacos.provider.user;


import net.quanter.sample.nacos.api.UserService;
import net.quanter.sample.nacos.dto.ResultDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

@Service("HelloServiceImpl")
@RestController
public class UserServiceImpl implements UserService {

    @Resource
    UserRepository userRepository;

    @Override
    public ResultDTO<UserDTO> get(Integer id) {
        Optional<User> userOptional = userRepository.findById(id);
        if(userOptional.isPresent()){
            User user = userOptional.get();
            UserDTO userDTO = convert(user);
            return ResultDTO.success(userDTO);
        }
        return ResultDTO.fail("未找到user:"+id);
    }

    public static UserDTO convert(User user) {
        if (user == null) {
            return null;
        }
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setName(user.getName());
        return userDTO;
    }
}
